Skip to content

Preserve AbortSignal.any cancellation before source listeners run - #59464

Open
Marve10s wants to merge 2 commits into
react:mainfrom
Marve10s:fix/abort-signal-dependent-cancellation
Open

Marve10s wants to merge 2 commits into
react:mainfrom
Marve10s:fix/abort-signal-dependent-cancellation

Conversation

@Marve10s

@Marve10s Marve10s commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

AbortSignal.any() currently connects its inputs through public abort listeners. An earlier listener can stop event propagation and leave the combined signal active after its source is cancelled. It can also observe stale cancellation state, or trigger a second source that incorrectly replaces the first abort reason.

Track dependent signals internally and mark all of them aborted before dispatching the source's event. Dispatch the dependent events afterward, in registration order. Flattening nested combinations preserves the same ordering through chains; weak source references and cleanup avoid retaining inputs after cancellation.

This follows the DOM signal-abort algorithm. The failures reproduce headlessly on React Native 0.88.0 and current main.

const first = new AbortController();
const second = new AbortController();
first.signal.addEventListener('abort', () => second.abort('second'));
const combined = AbortSignal.any([first.signal, second.signal]);
first.abort('first');
console.log(combined.reason);

Before: second. After: first, matching native behavior.

Changelog:

[GENERAL] [FIXED] - Preserve AbortSignal.any() cancellation state and the first abort reason before source abort listeners run.

Test Plan:

The regression tests live in the existing Fantom suite, packages/react-native/src/private/webapis/dom/abort-api/__tests__/AbortController-itest.js, under 'any' static method. They cover propagation suppression, state during the source event, nested event order, and the reentrant first reason. Each runs inside Fantom.runTask because Fantom requires WeakRef construction and dereferencing inside its event loop.

The same four cases fail against the unchanged main implementation and pass with this change when run headlessly under Jest. Two extra probes passed: a dependent aborted by one source ignores a later abort of its other source, and duplicate inputs dispatch once.

./node_modules/.bin/eslint --max-warnings 0 packages/react-native/src/private/webapis/dom/abort-api/AbortSignal.js packages/react-native/src/private/webapis/dom/abort-api/__tests__/AbortController-itest.js
./node_modules/.bin/prettier --check packages/react-native/src/private/webapis/dom/abort-api/AbortSignal.js packages/react-native/src/private/webapis/dom/abort-api/__tests__/AbortController-itest.js
npx --yes --package=flow-bin@0.340.0 flow focus-check --max-workers 2 packages/react-native/src/private/webapis/dom/abort-api/AbortSignal.js packages/react-native/src/private/webapis/dom/abort-api/__tests__/AbortController-itest.js

ESLint, formatting, and focused Flow checking pass. Fantom was not run locally and relies on CI here. Native builds, the full Jest suite, and full-repository Flow checking were not run.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 25, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Oct 25, 2026
@javache
javache requested a review from rubennorte November 4, 2026 09:41
@Marve10s
Marve10s force-pushed the fix/abort-signal-dependent-cancellation branch from ed69f9f to 8b503c7 Compare November 16, 2026 14:28
@Marve10s

Copy link
Copy Markdown
Contributor Author

Note

🤖 Claude Opus 5.5 commenting on behalf of Ibrahim (@Marve10s)

Thanks @rubennorte! Moved the tests into AbortController-itest.js and switched dependents to Set<WeakRef<AbortSignal>>. I kept the source refs: they let nested any() calls flatten to the real sources, and let an aborted dependent remove itself from its other sources so long-lived signals don't pile up dead refs. Happy to drop them if you'd prefer.

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Visual regression

No visual differences at 069b4633f867.

@Marve10s
Marve10s force-pushed the fix/abort-signal-dependent-cancellation branch from 8b503c7 to 069b463 Compare November 20, 2026 11:17

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants